package com.bc.bcapi.service;

import com.bc.bcmodel.domain.T_User_buyer_infoModel;
import com.bc.bcmodel.domain.T_User_seller_infoModel;
import com.bc.bcmodel.enums.SendCoinEnum;
import com.bc.commoncore.util.DateUtil;
import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.common.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.math.BigInteger;

import com.bc.bcapi.dao.T_Sendcoin_logMapper;
import com.bc.bcmodel.domain.T_Sendcoin_logModel;

/**
 * @Name T_Sendcoin_logService * @Description  * @Author generation * @Date 2018/11/3 19:23:42 * @Version 1.0
 **/
@Service
public class T_Sendcoin_logService {
    @Resource
    T_Sendcoin_logMapper t_Sendcoin_logMapper;
    @Resource
    T_User_buyer_infoService t_user_buyer_infoService;
    @Resource
    T_User_seller_infoService t_user_seller_infoService;
    @Resource
    ISTService istService;

    /**
     * 保存单条数据     *      * @return
     */
    public int insert(T_Sendcoin_logModel req) {
        return t_Sendcoin_logMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */
    public int insertBatch(List<T_Sendcoin_logModel> req) {
        return t_Sendcoin_logMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    public T_Sendcoin_logModel findByNamedParam(T_Sendcoin_logModel req) {
        return t_Sendcoin_logMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */
    public List<T_Sendcoin_logModel> getAllList(T_Sendcoin_logModel req) {
        return t_Sendcoin_logMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */
    public PageInfo<T_Sendcoin_logModel> getList(T_Sendcoin_logModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_Sendcoin_logModel> list = t_Sendcoin_logMapper.findByNamedParamList(req);
        return new PageInfo<T_Sendcoin_logModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */
    public int update(T_Sendcoin_logModel req) {
        return t_Sendcoin_logMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */
    public int delete(BigInteger id) {
        return t_Sendcoin_logMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    public long count(T_Sendcoin_logModel req) {
        return t_Sendcoin_logMapper.count(req);
    }

    //设置增加ist记录
    public void setSendCoin(SendCoinEnum sendCoinEnum, String user_id, String bus_id) {
        int coinData = sendCoinEnum.value();
        T_Sendcoin_logModel t_sendcoin_logModel = new T_Sendcoin_logModel();
        t_sendcoin_logModel.setBus_id(bus_id);
        t_sendcoin_logModel.setUser_id(user_id);
        t_sendcoin_logModel.setType_info(sendCoinEnum.desc());
        int count = t_Sendcoin_logMapper.count(t_sendcoin_logModel);
        if (count > 0) {
            return;
        }
        if (sendCoinEnum.value() == SendCoinEnum.REGISTERSUCCESS.value()) {
            //注册成功并通过KYC
            //当前用户送100
            T_Sendcoin_logModel registerLog = new T_Sendcoin_logModel();
            registerLog.setUser_id(user_id);
            registerLog.setBus_id(bus_id);
            registerLog.setIst_price(new BigDecimal(100));
            registerLog.setPlatform("c");
            registerLog.setType_info(SendCoinEnum.REGISTERSUCCESS.desc());
            t_Sendcoin_logMapper.insert(registerLog);
            //查询是否有推荐，买家用户送50
            String invote_id = getBuyerInvoteUserId(user_id);
            if (StringUtils.isEmpty(invote_id)) {
                return;
            }
            T_Sendcoin_logModel invoteRegister = new T_Sendcoin_logModel();
            invoteRegister.setUser_id(invote_id);
            invoteRegister.setBus_id(bus_id);
            invoteRegister.setIst_price(new BigDecimal(50));
            invoteRegister.setPlatform("c");
            invoteRegister.setType_info("邀请用户注册");
            t_Sendcoin_logMapper.insert(invoteRegister);
        } else if (sendCoinEnum.value() == SendCoinEnum.PROJECTSUCCESS.value()) {
            //项目方入驻成功
            //卖家送600
            T_Sendcoin_logModel registerLog = new T_Sendcoin_logModel();
            registerLog.setUser_id(user_id);
            registerLog.setBus_id(bus_id);
            registerLog.setIst_price(new BigDecimal(600));
            registerLog.setPlatform("b");
            registerLog.setType_info(SendCoinEnum.PROJECTSUCCESS.desc());
            t_Sendcoin_logMapper.insert(registerLog);
            //买家推荐人送300

            String invote_id = getSellerInvoteUserId(user_id);
            if (StringUtils.isEmpty(invote_id)) {
                return;
            }
            T_Sendcoin_logModel invoteRegister = new T_Sendcoin_logModel();
            invoteRegister.setUser_id(invote_id);
            invoteRegister.setBus_id(bus_id);
            invoteRegister.setIst_price(new BigDecimal(300));
            invoteRegister.setPlatform("c");
            invoteRegister.setType_info("邀请项目入驻");
            t_Sendcoin_logMapper.insert(invoteRegister);
        } else if (sendCoinEnum.value() == SendCoinEnum.ELITESSUCCESS.value()) {
//            成功成为专家送600
            T_Sendcoin_logModel elitesLog = new T_Sendcoin_logModel();
            elitesLog.setUser_id(user_id);
            elitesLog.setBus_id(bus_id);
            elitesLog.setIst_price(new BigDecimal(600));
            elitesLog.setPlatform("c");
            elitesLog.setType_info(SendCoinEnum.ELITESSUCCESS.desc());
            t_Sendcoin_logMapper.insert(elitesLog);
        }
    }

    /**
     * 获取卖家推荐人
     */
    private String getSellerInvoteUserId(String user_id) {
        T_User_seller_infoModel t_user_seller_infoModel = new T_User_seller_infoModel();
        t_user_seller_infoModel.setUser_id(user_id);
        T_User_seller_infoModel result = t_user_seller_infoService.findByNamedParam(t_user_seller_infoModel);
        if (result == null) {
            return "";
        }
        return result.getInvite_code();
    }

    /**
     * 获取买家推荐人
     */
    private String getBuyerInvoteUserId(String user_id) {
        T_User_buyer_infoModel t_user_buyer_infoModel = new T_User_buyer_infoModel();
        t_user_buyer_infoModel.setUser_id(user_id);
        T_User_buyer_infoModel result = t_user_buyer_infoService.findByNamedParam(t_user_buyer_infoModel);
        if (result == null) {
            return "";
        }
        return result.getInvite_code();
    }

    /**
     * 自动发币
     */
    public JsonResult sendCoin() {
        List<T_Sendcoin_logModel> list = t_Sendcoin_logMapper.getData();
        for (T_Sendcoin_logModel item :
                list) {
            String user_id = item.getUser_id();
            BigDecimal amount = item.getIst_price();

            try {
                T_Sendcoin_logModel t_sendcoin_logModel = new T_Sendcoin_logModel();
                t_sendcoin_logModel.setId(item.getId());
                t_sendcoin_logModel.setIs_pay(1);
                t_Sendcoin_logMapper.update(t_sendcoin_logModel);
                istService.payIST(user_id, amount);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;

    }
}